//
//  StrategyViewController.m
//  课余时间
//
//  Created by zgr on 16/4/9.
//  Copyright © 2016年 张大少. All rights reserved.
//

#import "StrategyViewController.h"
#import "RequestManager.h"
#import "StrategyModel.h"
#import "StrategyCollectionViewCell.h"
#import "ChildrenModel.h"
#import "SectionsModel.h"
#import "PhotosModel.h"
#import "StrategyDetailViewController.h"
@interface StrategyViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,strong)NSMutableArray *modelArray;
@property (nonatomic,strong)NSMutableArray *headArray;
@property (nonatomic,strong)NSMutableArray *sectionArray;
@end

@implementation StrategyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _modelArray = [NSMutableArray array];
    _headArray = [NSMutableArray array];
    _sectionArray = [NSMutableArray array];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, KHEIGHT - 64) collectionViewLayout:flowLayout];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [_collectionView registerClass:[StrategyCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    
    [self.view addSubview:_collectionView];
    
    [self requstDataWithUrlString:[NSString stringWithFormat:@"http://chanyouji.com/api/wiki/destinations/%@.json",_descID]];
    
    
    
    
    // Do any additional setup after loading the view.
}



-(void)requstDataWithUrlString:(NSString *)urlString
{
    
    [[[RequestManager alloc]init] requestWithUrl:urlString requestType:RequestTypeGet parDic:nil finish:^(NSData *data) {
        NSArray *bigArray = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        
        for (NSDictionary *dataDic in bigArray) {
            [_headArray addObject:[dataDic objectForKey:@"category_type"]];
            NSArray *pagesArray = [dataDic objectForKey:@"pages"];
            
            NSMutableArray *smallModelArray = [NSMutableArray array];
            for (NSDictionary *pageDic in pagesArray) {
                StrategyModel *model = [[StrategyModel alloc]init];
                [model setValuesForKeysWithDictionary:pageDic];
                [smallModelArray addObject:model];
            }
            
            [_modelArray addObject:smallModelArray];
            
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [_collectionView reloadData];
        });
       } error:^(NSError *error) {
        
    }];
    
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [_modelArray[section] count];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return _modelArray.count;
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        
        if (headerView != nil) {
        
            
            
            for (UILabel *title in headerView.subviews) {
                [title removeFromSuperview];
            }
        
        }
        UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(5, 2, 100, 30)];
        title.textColor = [UIColor greenColor];
        
       NSArray *typeNameArr = @[@"概述", @"出行须知", @"如何到达", @"当地交通", @"景点", @"娱乐", @"住宿", @"美食", @"购物", @"离境须知",@"其他", @"其他"];
        
        NSInteger i = [self.headArray[indexPath.section] integerValue];
        
        title.text = typeNameArr[i];
        [headerView addSubview:title];
        
        
        
        return headerView;
        
    }
    
    return nil;

}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(KWIDTH, 35);
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    StrategyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.model = _modelArray[indexPath.section][indexPath.row];
    return cell;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(KWIDTH/2 - 5, 30);
    
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    StrategyModel *model = _modelArray[indexPath.section][indexPath.item];
//    ChildrenModel *model1 = model.childrenModel[0];
//    SectionsModel *modelS = model1.sectionModel[0];
//  //  PhotosModel *modelP = modelS.photoModel[0];
//    NSLog(@"====%@",[modelS.photoModel[0] image_url] );
//    NSLog(@"model----%@",model.title);
//    NSLog(@"model1----%@",model1.title);
//    NSLog(@"modelS----%@",modelS.title);
 
    StrategyDetailViewController *straDetail = [[StrategyDetailViewController alloc]init];
    straDetail.index = indexPath.row;
    straDetail.strategyModel = model;
    [self.navigationController pushViewController:straDetail animated:YES];
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
